package whu.o2o.adapter;

import java.util.ArrayList;

import whu.o2o.util.ConstantData;
import whu.o2o.vo.PersonVO;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import whu.o2o.R;

public class GuideAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private ArrayList<PersonVO> mList;
	private Drawable mValidatedDrawable;
	private Drawable mManDrawable;
	private Drawable mWomanDrawable;

	public GuideAdapter(Context context, ArrayList<PersonVO> list) {
		mContext = context;
		mList = list;
		mLayoutInflater = LayoutInflater.from(mContext);

		mValidatedDrawable = mContext.getResources().getDrawable(R.drawable.attestation_v);
		mValidatedDrawable.setBounds(0, 0, mValidatedDrawable.getMinimumWidth(), mValidatedDrawable.getMinimumHeight());

		mManDrawable = mContext.getResources().getDrawable(R.drawable.boy);
		mManDrawable.setBounds(0, 0, mManDrawable.getMinimumWidth(), mManDrawable.getMinimumHeight());

		mWomanDrawable = mContext.getResources().getDrawable(R.drawable.girl);
		mWomanDrawable.setBounds(0, 0, mWomanDrawable.getMinimumWidth(), mWomanDrawable.getMinimumHeight());
	}

	public int getCount() {
		return mList.size();
	}

	public Object getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		PersonVO vo = mList.get(position);

		ViewHolder holder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.griditem_guide, null);

			holder = new ViewHolder();
			holder.head = (ImageView) convertView.findViewById(R.id.head);
			holder.cardImage = (ImageView) convertView.findViewById(R.id.card_image);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.introduction = (TextView) convertView.findViewById(R.id.introduction);
			holder.age = (TextView) convertView.findViewById(R.id.age);
			holder.level = (TextView) convertView.findViewById(R.id.level);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.name.setText(vo.getName());
		holder.introduction.setText(vo.getIntroduction());
		holder.age.setText(String.format(mContext.getResources().getString(R.string.age), vo.getAge()));
		holder.level.setText(String.format(mContext.getResources().getString(R.string.level), vo.getLevel()));

		if (vo.isValidated()) {
			holder.name.setCompoundDrawables(null, null, mValidatedDrawable, null);
		} else {
			holder.name.setCompoundDrawables(null, null, null, null);
		}

		if (ConstantData.Data.WOMAN == vo.getSex()) {
			holder.age.setCompoundDrawables(mWomanDrawable, null, null, null);
		} else {
			holder.age.setCompoundDrawables(mManDrawable, null, null, null);
		}

		return convertView;
	}
	
	static class ViewHolder {
		ImageView head;
		ImageView cardImage;
		TextView name;
		TextView introduction;
		TextView age;
		TextView level;
	}
}
